<?php

namespace Admin\Controller;

/**
 * 海报控制器
 * @author wangyuanlin
 * 2015-05-17
 */
class PosterController extends AdminController {

    /**
     * 海报列表
     * @author wangyuanlin
     * 2015-05-17
     */
    public function index() {
        $title = I('title');
        $map['title'] = array('like', '%' . (string) $title . '%');
        $list = $this->lists('poster', $map ,'createdate desc');
        int_to_string($list);  
        $this->assign('_list', $list);
        $this->meta_title = '数据列表';
        $this->display();
    }

    /**
     * 新增海报
     * @author zhangwei
     * 2015-07-2
     */
    public function add($title = "", $urlstr = "",$mold=null,$pid=0) {
        if (IS_POST) {
            // 上传文件 
            $setting = array(
                'mimes' => '', //允许上传的文件MiMe类型
                'maxSize' => 2 * 1024 * 1024, //上传的文件大小限制 (0-不做限制)
                'exts' => 'jpg,gif,png,jpeg', //允许上传的文件后缀
            );

            /* 调用文件上传组件上传文件 */
            /*             * 修改开始* */
            $pic_driver = C('PICTURE_UPLOAD_DRIVER');
            $upload = new \Think\Upload($setting, C('PICTURE_UPLOAD_DRIVER'), C("UPLOAD_{$pic_driver}_CONFIG"));
            $info = $upload->upload();

            if (!$info) {// 上传错误提示错误信息
                $this->error($upload->getError());
            } else {// 上传成功
                $data = array('title' => $title,'mold' => $mold,'urlstr' => $urlstr,'pid' => $pid, 'fileurl' => $info["photo"]["url"], 'createdate' => NOW_TIME, 'lastupdate' => NOW_TIME);
                $id = M('Poster')->add($data);
                if (!$id) {
                    $this->error("添加失败！");
                } else {
                    $this->success('添加成功！', U('index'));
                }
            }
        } else {
            $this->display();
        }
    }
    
    /**
     * 修改海报
     * @author wangyuanlin
     * 2015-05-17
     */
    public function edit($title = "", $urlstr = "",$mold=null,$pid=0) {
        $id = I('id');
        if (IS_POST) {
            
            // 上传文件 
            $setting = array(
                'mimes' => '', //允许上传的文件MiMe类型
                'maxSize' => 2 * 1024 * 1024, //上传的文件大小限制 (0-不做限制)
                'exts' => 'jpg,gif,png,jpeg', //允许上传的文件后缀
            );

            /* 调用文件上传组件上传文件 */
            /*             * 修改开始* */
            $pic_driver = C('PICTURE_UPLOAD_DRIVER');
            $upload = new \Think\Upload($setting, C('PICTURE_UPLOAD_DRIVER'), C("UPLOAD_{$pic_driver}_CONFIG"));
            $info = $upload->upload();
            
            if($info){
                $data = array('title' => $title,'mold' => $mold, 'fileurl' => $info["photo"]["url"], 'urlstr' => $urlstr,'pid' => $pid, 'lastupdate' => NOW_TIME);
            }else{
                $data = array('title' => $title,'mold' => $mold,'urlstr' => $urlstr,'pid' => $pid, 'lastupdate' => NOW_TIME);
            }
            
            if (!D('Poster')->where(array('id' => $id))->save($data)) {
                $this->error("修改失败！");
            } else {
                $this->success('修改成功！', U('index'));
            }
        } else {
            $data = M("Poster")->where(array('id' => $id))->find();
            $this->data = $data;
            $this->display();
        }
    }
    
    /**
     * 删除海报
     * @author zhangwei
     * 2015-07-2
     */
    public function del($id = 0) {
        if ($id) {
            $Poster = M("Poster");
            
            $Poster->status = -1;
            $reuslt = $Poster->where(array('id'=>$id))->save(); 
            
            if ($reuslt) {
                $this->success('删除成功！', U('index'));
            } else {
                $this->error("删除失败！");
            }
        }
    }
}
